package org.springblade.mes.logistics.entry.service;


import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.logistics.entry.entity.WarehouseEntry;
import org.springblade.mes.logistics.entry.mapper.WarehouseEntryMapper;
import org.springblade.mes.logistics.entry.model.WarehouseEntryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 服务
 *
 * @Version 1.0
 */
@Slf4j
@Service
@DS("logistics")
public class WarehouseEntryService extends BaseServiceImpl<WarehouseEntryMapper, WarehouseEntry> {

    @Transactional(rollbackFor = Exception.class)
    public boolean addData(WarehouseEntryBean param) throws Exception {
        //保存
        WarehouseEntry entity = param;
        return this.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateData(WarehouseEntryBean param) throws Exception {
        //更新
        WarehouseEntry newEntity = param;
        return this.updateById(newEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean deleteData(WarehouseEntryBean param) throws Exception {
        if (param.getIdList() != null && param.getIdList().size() > 0) {
            UpdateWrapper<WarehouseEntry> del = new UpdateWrapper<>();
            del.lambda().in(WarehouseEntry::getId, param.getIdList());
            return this.remove(del);
        }
        if (param.getId() != null) {
            return this.removeById(param.getId());
        }
        return true;
    }


    public WarehouseEntryBean findById(Long id) {
        WarehouseEntryBean cond = new WarehouseEntryBean();
        cond.setId(id);
        return findBySpec(cond);
    }


    public WarehouseEntryBean findBySpec(WarehouseEntryBean param) {
        List<WarehouseEntryBean> list = this.baseMapper.queryList(param);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }


    public List<WarehouseEntryBean> findListBySpec(WarehouseEntryBean param) {
        return this.baseMapper.queryList(param);
    }


    public IPage<WarehouseEntryBean> findPageBySpec(IPage<WarehouseEntryBean> page, WarehouseEntryBean param) {
        List<WarehouseEntryBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }


    public void refreshUpdateTime(String tenantId, Date time) {
        WarehouseEntryBean entity = new WarehouseEntryBean();
        entity.setTenantId(tenantId);
        entity.setCreateTime(DateUtil.offsetDay(time,-5));
        baseMapper.refreshUpdateTime(entity);
    }
}